package main

import (
	"io"
	"bytes"
	"fmt"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
)

//如果你还有其他普通字段例如username之类的需要同时写入，
//那么可以调用multipart的WriteField方法写很多其他类似的字段。

func postFile(filename string,targetUrl string)error{
	bodyBuf:=&bytes.Buffer{}
	bodyWriter:=multipart.NewWriter(bodyBuf)
	
	//关键的一步操作
	fileWriter,err:=bodyWriter.CreateFormFile("uploadfile",filename)
	if err != nil{
		fmt.Println("error when writing to buffer...")
		return err
	}
	
	//打开文件句柄操作
	fh,err:=os.Open(filename)
	if err!=nil{
		fmt.Println("error when opening file",filename)
		return err
	}
	defer fh.Close()
	
	//iocopy
	_,err=io.Copy(fileWriter,fh)
	if err!=nil{
		fmt.Println("error when copying")
		return err
	}
	
	contentType:=bodyWriter.FormDataContentType()
	bodyWriter.Close()
	
	resp,err:=http.Post(targetUrl,contentType,bodyBuf)
	if err!=nil{
		return err
	}
	defer resp.Body.Close()
	resp_body,err:=ioutil.ReadAll(resp.Body)
	if err!=nil{
		return err
	}
	fmt.Println("",resp.Status)
	fmt.Println(string(resp_body))
	return nil
	
}

func main(){
	target_url:="http://localhost:9090/upload"
	file_name:="login.gtpl"
	postFile(file_name,target_url)	
}